<?php
	include 'php/config.php';
	session_start();
	if (!isset($_SESSION["id_utente"])) {
		header("Location: " . $ISSUER_HOST . "/login.php");
		exit();
	} else if (!$_SESSION["admin"]) {
		header("Location: " . $ISSUER_HOST . "/forbidden.html");
		exit();
	}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF8">
<script src="js/utils.js"></script>
<script src="js/insert_badge.js"></script>
<title>Insert Badge</title>
</head>
<body>
<h1>New Badge</h1>
	<div>
	<p>Da questa pagina è possibile creare una nuova badge</p>
	</div>
	<?php
		// Connecting, selecting database
		$mysqli = new mysqli($MYSQL_HOST, $MYSQL_USERNAME, $MYSQL_PASSWORD, $MYSQL_DB_NAME);
		if ($mysqli->connect_errno) {
			echo "<p>Failed to connect to MySQL: " . $mysqli->connect_error . "<p>";
		} else {
			$query = "SELECT `nome`,`idCategoria` FROM `categoria`";
			$result = $mysqli->query($query);
			if (!$result) {
				echo "<p>Failed to run query " . $query . ": (" . $mysqli->errno . ") " .
						$mysqli->error . "<p>";
			}
		}
			
	?>
	<form action="php/scripts/check_criteria.php" method="post" enctype="multipart/form-data">
	<div>
	<label>Crea una badge</label>
	</div>
	<table border="0">
		<tr>
			<td><label>Nome Badge</label></td>
			<td><input id="name" name="nome" type="text" maxLength="70" oninput="validateNewBadgeForm()" /></td>
		</tr>
		<tr>
			<td><label>Descrizione</label></td>
			<td><textarea id="description" name="descrizione" rows="1" cols="70" oninput="validateNewBadgeForm()"></textarea></td>
		</tr>
		<tr>
			<td><label>Immagine</label></td>
			<td><input type="file" name="immagine" id="file" /></td>
		</tr>
		<tr>
			<td><label>Tags</label></td>
			<td><input id="tags" name="tags" type="text" maxLength="70" /></td>
		</tr>
		<tr>
			<td><label>Condivisa</label></td>
			<td><input id="condivisa" name="condivisa" type="checkbox" value="condivisa" /></td>
		</tr>
		<tr>
			<td><label>Categoria</label></td>
			<td><select id="categoria" name="categoria">
			<option value="">Scegli una categoria</option>
			<?php
				while ($row = $result->fetch_assoc()) {
					echo "<option value=" . $row['idCategoria'] . ">" .
					 $row['nome'] . " </option>";
				}
				mysqli_free_result($result);		
				$mysqli->close();
			?>
			</select></td>
		</tr>
		<tr>
			<td><label>Criteria</label></td>
			<td><input id="criteria" name="criteria" type="checkbox" value="criteria" /></td>
		</tr>
	</table>
	<div id="message"></div>
	<input id="create" type="submit" value="Crea" disabled="disabled" />
	</form>
	<div>
	<p>Torna alla <a href="index.html">Home</a></p>
	</div>
</body>
</html>